package day07;

import java.io.*;

/**
 * @author zouran
 * createDate：2024/6/28  19:08
 */
public class FileRead {
    public static void example01() throws IOException {
        InputStream in = new FileInputStream("example.txt");
        byte[] bytes = new byte[1024];
        int len = 0;
        StringBuilder strings = new StringBuilder();
        while ((len = in.read(bytes)) != -1) {
            strings.append(new String(bytes, 0, len));
        }
        in.close();
        System.out.println(strings);
    }

    public static void example02() throws IOException {
        FileReader fileReader = new FileReader("example.txt");
        char[] chars = new char[1024];
        int len;
        StringBuilder strings = new StringBuilder();
        while ((len = fileReader.read(chars)) != -1) {
            strings.append(new String(chars, 0, len));
        }
        fileReader.close();
        System.out.println(strings);
    }

    public static void example03() throws IOException {
        FileReader fileReader = new FileReader("example.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String s;
        while ((s = bufferedReader.readLine()) != null) {
            System.out.println(s);
        }
        fileReader.close();
        bufferedReader.close();
    }

    public static void example04() throws IOException {
        InputStream in = new FileInputStream("example.txt");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
        byte[] bytes = new byte[1024];
        int len;
        StringBuilder strings = new StringBuilder();
        while ((len = bufferedInputStream.read(bytes)) != -1) {
            strings.append(new String(bytes, 0, len));
        }
        in.close();
        bufferedInputStream.close();
        System.out.println(strings);
    }

    public static void main(String[] args) throws IOException {
        example04();
    }
}
